package cn.servlet;

import cn.entity.User;
import cn.service.UserService;
import cn.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        User user = userService.login(name,password);
        String msg = "";
        if (null==user){
            request.setAttribute("msg","用户不存在");
            request.getRequestDispatcher("/rentingSys/login.jsp").forward(request,response);
        }else if(!user.getPassword().equals(password)){
            request.setAttribute("msg","用户密码错误");
            request.getRequestDispatcher("/rentingSys/login.jsp").forward(request,response);
        }else {
            request.getSession().setAttribute("user",user);
            response.sendRedirect("/rentingSys/list.jsp");

        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        doPost(request,response);
    }
}
